{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pandas import DataFrame,Series\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "  name  id  age\n0   张三   1   22\n1   李四   2   33\n2   王五   3   44",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>id</th>\n      <th>age</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>张三</td>\n      <td>1</td>\n      <td>22</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>李四</td>\n      <td>2</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>王五</td>\n      <td>3</td>\n      <td>44</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "   id sex        job\n0   2   男      saler\n1   3   女        CTO\n2   4   女  Programer",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>id</th>\n      <th>sex</th>\n      <th>job</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2</td>\n      <td>男</td>\n      <td>saler</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>3</td>\n      <td>女</td>\n      <td>CTO</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4</td>\n      <td>女</td>\n      <td>Programer</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df1 = pd.DataFrame({\n",
    "    'name': ['张三', '李四', '王五'],\n",
    "    'id' : [1, 2, 3],\n",
    "    'age': [22, 33, 44]\n",
    "})\n",
    "df2 = pd.DataFrame({\n",
    "    'id' : [2, 3, 4],\n",
    "    'sex': ['男', '女', '女'],\n",
    "    'job': ['saler', 'CTO', 'Programer']\n",
    "})\n",
    "display(df1, df2)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "  name  id  age sex    job\n0   李四   2   33   男  saler\n1   王五   3   44   女    CTO",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>id</th>\n      <th>age</th>\n      <th>sex</th>\n      <th>job</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>李四</td>\n      <td>2</td>\n      <td>33</td>\n      <td>男</td>\n      <td>saler</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>王五</td>\n      <td>3</td>\n      <td>44</td>\n      <td>女</td>\n      <td>CTO</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.merge(df2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "  name  id  age sex    job\n0   李四   2   33   男  saler\n1   王五   3   44   女    CTO",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>id</th>\n      <th>age</th>\n      <th>sex</th>\n      <th>job</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>李四</td>\n      <td>2</td>\n      <td>33</td>\n      <td>男</td>\n      <td>saler</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>王五</td>\n      <td>3</td>\n      <td>44</td>\n      <td>女</td>\n      <td>CTO</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.merge(df2,on=\"id\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "  name  id  age\n0   张三   1   22\n1   李四   2   33\n2   王五   3   44",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>id</th>\n      <th>age</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>张三</td>\n      <td>1</td>\n      <td>22</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>李四</td>\n      <td>2</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>王五</td>\n      <td>3</td>\n      <td>44</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "   id name        job\n0   2   李四      saler\n1   3   王五        CTO\n2   4   赵六  Programer",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>id</th>\n      <th>name</th>\n      <th>job</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2</td>\n      <td>李四</td>\n      <td>saler</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>3</td>\n      <td>王五</td>\n      <td>CTO</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4</td>\n      <td>赵六</td>\n      <td>Programer</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df1 = pd.DataFrame({\n",
    "    'name': ['张三', '李四', '王五'],\n",
    "    'id' : [1, 2, 3],\n",
    "    'age': [22, 33, 44]\n",
    "})\n",
    "df2 = pd.DataFrame({\n",
    "    'id' : [2, 3, 4],\n",
    "    'name': ['李四', '王五', '赵六'],\n",
    "    'job': ['saler', 'CTO', 'Programer']\n",
    "})\n",
    "display(df1,df2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "  name_x  id  age name_y    job\n0     李四   2   33     李四  saler\n1     王五   3   44     王五    CTO",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name_x</th>\n      <th>id</th>\n      <th>age</th>\n      <th>name_y</th>\n      <th>job</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>李四</td>\n      <td>2</td>\n      <td>33</td>\n      <td>李四</td>\n      <td>saler</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>王五</td>\n      <td>3</td>\n      <td>44</td>\n      <td>王五</td>\n      <td>CTO</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.merge(df2)\n",
    "\n",
    "df1.merge(df2,on=\"id\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "  name_x  id_x  age  id_y name_y        job\n0     张三     1   22     2     李四      saler\n1     李四     2   33     3     王五        CTO\n2     王五     3   44     4     赵六  Programer",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name_x</th>\n      <th>id_x</th>\n      <th>age</th>\n      <th>id_y</th>\n      <th>name_y</th>\n      <th>job</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>张三</td>\n      <td>1</td>\n      <td>22</td>\n      <td>2</td>\n      <td>李四</td>\n      <td>saler</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>李四</td>\n      <td>2</td>\n      <td>33</td>\n      <td>3</td>\n      <td>王五</td>\n      <td>CTO</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>王五</td>\n      <td>3</td>\n      <td>44</td>\n      <td>4</td>\n      <td>赵六</td>\n      <td>Programer</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.merge(df2,left_index=True,right_index=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "   id name_x  id_x  age  id_y name_y    job\n0   2     王五     3   44     2     李四  saler",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>id</th>\n      <th>name_x</th>\n      <th>id_x</th>\n      <th>age</th>\n      <th>id_y</th>\n      <th>name_y</th>\n      <th>job</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2</td>\n      <td>王五</td>\n      <td>3</td>\n      <td>44</td>\n      <td>2</td>\n      <td>李四</td>\n      <td>saler</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.merge(df2,left_index=True,right_on=\"id\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "  name  id  age\n0   张三   1   22\n1   李四   2   33\n2   王五   3   44",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>id</th>\n      <th>age</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>张三</td>\n      <td>1</td>\n      <td>22</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>李四</td>\n      <td>2</td>\n      <td>33</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>王五</td>\n      <td>3</td>\n      <td>44</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "   id sex        job\n0   2   男      saler\n1   3   女        CTO\n2   4   女  Programer",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>id</th>\n      <th>sex</th>\n      <th>job</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2</td>\n      <td>男</td>\n      <td>saler</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>3</td>\n      <td>女</td>\n      <td>CTO</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4</td>\n      <td>女</td>\n      <td>Programer</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df1 = pd.DataFrame({\n",
    "    'name': ['张三', '李四', '王五'],\n",
    "    'id' : [1, 2, 3],\n",
    "    'age': [22, 33, 44]\n",
    "})\n",
    "df2 = pd.DataFrame({\n",
    "    'id' : [2, 3, 4],\n",
    "    'sex': ['男', '女', '女'],\n",
    "    'job': ['saler', 'CTO', 'Programer']\n",
    "})\n",
    "display(df1, df2)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "  name  id   age  sex        job\n0   张三   1  22.0  NaN        NaN\n1   李四   2  33.0    男      saler\n2   王五   3  44.0    女        CTO\n3  NaN   4   NaN    女  Programer",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>id</th>\n      <th>age</th>\n      <th>sex</th>\n      <th>job</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>张三</td>\n      <td>1</td>\n      <td>22.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>李四</td>\n      <td>2</td>\n      <td>33.0</td>\n      <td>男</td>\n      <td>saler</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>王五</td>\n      <td>3</td>\n      <td>44.0</td>\n      <td>女</td>\n      <td>CTO</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>NaN</td>\n      <td>4</td>\n      <td>NaN</td>\n      <td>女</td>\n      <td>Programer</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.merge(df2,how=\"inner\")\n",
    "df1.merge(df2,how=\"left\")\n",
    "df1.merge(df2,how=\"right\")\n",
    "df1.merge(df2,how=\"outer\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "   id name  id_a  age  id_b sex    job\n0   2   王五     3   44     2   男  saler",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>id</th>\n      <th>name</th>\n      <th>id_a</th>\n      <th>age</th>\n      <th>id_b</th>\n      <th>sex</th>\n      <th>job</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2</td>\n      <td>王五</td>\n      <td>3</td>\n      <td>44</td>\n      <td>2</td>\n      <td>男</td>\n      <td>saler</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.merge(df2,left_index=True,right_on=\"id\",suffixes=[\"_a\",\"_b\"])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}